package xu.zhi.qing.follow.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xu.zhi.qing.common.entity.Pagination;
import xu.zhi.qing.common.service.BaseService;
import xu.zhi.qing.follow.entity.Follow;
import xu.zhi.qing.user.entity.User;

import java.util.*;

@Transactional
@Service
public class FollowService extends BaseService<Follow> {

	public JSONObject getFansList(User user,Integer limit,Integer page) {
		List<Follow> fansList=null;
		Pagination<Follow> pagination=null;
		Map<String,Integer> params = new HashMap<>();
		params.put("userId",user.getId());
		params.put("limit",limit);
		params.put("page",page);
		fansList = followDao.getFansListByUserId(params);
		JSONArray jFansList=new JSONArray();
		//需要获取粉丝的用户名，头像，粉丝数量，关注数量，性别
		for (Follow follow : fansList) {
			JSONObject jFans=new JSONObject();

			User fans=userDao.findById(follow.getFansId());
			jFans.put("id", fans.getId());
			jFans.put("username", fans.getUsername());
			jFans.put("userImg", fans.getImgPath());
			jFans.put("gender", fans.getGender());
			jFans.put("fansCount", followDao.countByUserId(fans.getId()));
			jFans.put("followCount", followDao.countByFansId(fans.getId()));
			jFans.put("shareCount", shareDao.countByUserId(fans.getId()));
			jFansList.add(jFans);
		}
		JSONObject result=new JSONObject();
		result.put("fansList", jFansList);
		if (page!=null&&pagination!=null) {
			result.put("page", page);
			result.put("rows", limit);
			result.put("count", pagination.getCount());
		}
		return result;
	}

	public JSONObject getFollowingList(User fans,Integer limit,Integer page) {
		List<Follow> followList=null;
		Pagination<Follow> pagination=null;
		Map<String,Integer> params = new HashMap<>();
		params.put("userId",fans.getId());
		params.put("limit",limit);
		params.put("page",page);
		followList = followDao.getFansListByUserId(params);
		JSONArray jUserList=new JSONArray();
		//需要获取粉丝的用户名，头像，粉丝数量，关注数量，性别
		for (Follow follow : followList) {
			JSONObject jUser=new JSONObject();

			User user=userDao.findById(follow.getUserId());
			jUser.put("id", user.getId());
			jUser.put("username", user.getUsername());
			jUser.put("userImg", user.getImgPath());
			jUser.put("gender", user.getGender());
			jUser.put("fansCount", followDao.countByUserId(user.getId()));
			jUser.put("followCount", followDao.countByFansId(user.getId()));
			jUser.put("shareCount", shareDao.countByUserId(user.getId()));
			jUserList.add(jUser);
		}

		JSONObject result=new JSONObject();
		result.put("followingList", jUserList);
		if (page!=null&&pagination!=null) {
			result.put("page", page);
			result.put("rows", limit);
			result.put("count", pagination.getCount());
		}

		return result;
	}

	public boolean isFollow(int id1, int id2) {
		if(id1==id2){
			return false;
		}
		User user1 = userDao.get(id1);
		Set<Follow> users = new HashSet<>(followDao.findByFansId(user1.getId()));
		for (Follow follow : users) {
			if (follow.getUserId().equals(id2)) {
				return true;
			}
		}
		return false;
	}
}
